package com.sdwz.borrow.controller;


import com.sdwz.borrow.client.UserClient;
import com.sdwz.borrow.entity.vo.BorrowInfoVo;
import com.sdwz.borrow.service.BorrowInfoService;
import com.sdwz.commonutil.Result;
import com.sdwz.serviceutil.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 借阅信息表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/api/borrow")
public class BorrowInfoApiController {

    @Autowired
    BorrowInfoService borrowInfoService;

    /**
     * 获取当前用户借阅中的借阅记录
     * @param request
     * @return
     */
    @GetMapping("/auth/current")
    public Result getBorrowingInfo(HttpServletRequest request){
        Long userId = JwtUtil.getUserId(request.getHeader("token"));
        List<BorrowInfoVo> result =  borrowInfoService.getBorrowingInfo(userId);
        return Result.ok(result);
    }

    /**
     * 获取当前用户的借阅历史
     * @param request
     * @return
     */
    @GetMapping("/auth/history/{current}/{limit}")
    public Result getBorrowHistoryInfo(HttpServletRequest request,
                                       @PathVariable Long current,
                                       @PathVariable Long limit){
        String userId = String.valueOf(JwtUtil.getUserId(request.getHeader("token")));
        Map<String,Object> result = borrowInfoService.getBorrowHistoryInfo(userId,current,limit);
        return Result.ok(result);
    }

    /**
     * 还书
     * @param request
     * @param bookId
     * @return
     */
    @PostMapping("/auth/toReturn/{bookId}")
    public Result toReturnBook(HttpServletRequest request,
                               @PathVariable String bookId){
        String userId = String.valueOf(JwtUtil.getUserId(request.getHeader("token")));
        borrowInfoService.toReturnBook(userId,bookId);
        return Result.ok();
    }

    /**
     * 归还超时书籍
     * @param request
     * @param bookId
     * @return
     */
    @PostMapping("/auth/toReturnTimeOut/{bookId}")
    public Result toReturnTimeOutBook(HttpServletRequest request,
                               @PathVariable String bookId){
        String userId = String.valueOf(JwtUtil.getUserId(request.getHeader("token")));
        borrowInfoService.toReturnTimeOutBook(userId,bookId);
        return Result.ok();
    }

}

